package com.example.hospital.service.impl;


import com.example.hospital.exception.DepartmentServiceException;
import com.example.hospital.mapper.DepartmentMapper;
import com.example.hospital.pojo.Department;
import com.example.hospital.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    // 添加科室
    @Override
    public void addDepartment(Department department) {
        if(departmentMapper.findByName(department).getName().equals(department.getName())){
            throw new DepartmentServiceException("The department already exists!");
        }
        departmentMapper.insertDepartment(department);
    }

    // 获取所有科室
    @Override
    public List<Department> getAllDepartments() {
        return departmentMapper.findAll();
    }

    // 根据ID获取科室
    @Override
    public Department getDepartmentById(Integer id) {
        return departmentMapper.findById(id);
    }

    // 更新科室
    @Override
    public void updateDepartment(Department department) {
        departmentMapper.updateDepartment(department);
    }

    // 删除科室
    @Override
    public void deleteDepartment(Integer id) {
        departmentMapper.deleteDepartment(id);
    }
}
